package Servlet;

import DAO.UserDao;
import DataClass.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addUserServlet")
public class AddUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String user_name=request.getParameter("user_name");
        String password=request.getParameter("password");
        System.out.println(user_name);
        System.out.println(password);
        User u=new User(user_name,password);
        UserDao dao=new UserDao();
        try{
            boolean f=dao.Insert(u);
            if(f==true){
                request.getRequestDispatcher("add_user_success.jsp").forward(request,response);
            }
            else{
                request.getRequestDispatcher("add_user_false.jsp").forward(request,response);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Serve at:").append(request.getContextPath());
    }
}
